<?php


namespace Api\Controller;


class PingxxController extends BaseController
{
    public function getParams($order_id,$channel,$aSubject,$aBody){
        $order = D('Api/Store')->getOrderById($order_id);


        if($order['condition'] == 1){
            $this->apiError('该订单已经支付');
        }
        if($order['uid'] != is_login()){
            $this->apiError('不是您的订单');
        }
        return array('channel'=>$channel,'body'=>$aBody,'subject'=>$aSubject,'amount'=>$this->getAmount($order));
    }


    public function getAmount($order){

        $finally =$order['total_cny'] + $order['adj_cny'];
        $score_id=modC('CURRENCY_TYPE',4,'Store');
        $fields_config = modC('RE_FIELD', "", 'Pingxx');
        $fields = json_decode($fields_config,true);
        $type = array_search_key($fields,'FIELD',$score_id);
        !$type && $type['UNIT'] =1;
        return number_format($finally/$type['UNIT'], 2, ".", "");
    }


    public function pay()
    {

        require_once('./Application/Pingxx/Lib/Pingxx/init.php');

       $this->requireIsLogin();
        $channel = I_POST('channel');
        if(empty($channel)){
            $channel='alipay';
        }
        $sub=modC('WEB_SITE_NAME', 'OpenSNS开源社交系统', 'Config').'微店在线支付';
        $aSubject = I_POST('subject');
        if(empty($aSubject)){
            $aSubject=$sub;
        }
        $body='通过'.$this->get_pay_method($channel).'进行微店商品在线支付';
        $aBody = I_POST('body');
        if(empty($aBody)){
            $aBody=$body;
        }

        $orderNo = I_POST('order_id');
        $alipay_open_id=I_POST('alipay_open_id');
        $link =M('order_link')->where(array('order_id' => $orderNo))->find();
        $data = $this->getParams($link['order_id'],$channel,$aSubject,$aBody);

        $channel = $data['channel'];
        $subject = $data['subject'];
        $body = $data['body'];
        $amount = $data['amount'];


        //$extra 在使用某些渠道的时候，需要填入相应的参数，其它渠道则是 array() .具体见以下代码或者官网中的文档。其他渠道时可以传空值也可以不传。
        $extra = array();

        // $redirect_url = U('pingxx/pingxx1/payCallback','',true,true);
        $redirect_url = 'http://' . str_replace('//', '/', $_SERVER['HTTP_HOST'] . __ROOT__ . '/pingxx/callback') . '.php';

        switch ($channel) {
            case 'alipay_pc_direct':
                $extra = array(
                    'success_url' => $redirect_url,
                );
                break;

            case 'alipay_wap':
                $extra = array(
                    'success_url' => $redirect_url,
                    'cancel_url' => $redirect_url
                );
                break;

            case 'alipay':
                $extra = array(
                    'extern_token'=>$alipay_open_id,
                );
                break;

        }

        \Pingpp\Pingpp::setApiKey(modC('SECRET_KEY', '', 'Pingxx'));

        try {
            $assign = array(
                'subject' => $subject,
                'body' => $body,
                'amount' => $amount * 100,
                'order_no' => $orderNo,
                'currency' => 'cny',
                'extra' => $extra,
                'channel' => $channel,
                'client_ip' => $_SERVER['REMOTE_ADDR'],
                'app' => array('id' => modC('APP_ID', '', 'Pingxx'))
            );

//
//          $ch=  \Pingpp\Charge::create($assign);
//dump($ch);
//          $data=array('data'=>serialize($ch));
//
//          $this->apiSuccess($data);
            $this->apiSuccess($assign);
            //echo $ch;
        } catch (\Pingpp\Error\Base $e) {
            header('Status: ' . $e->getHttpStatus());
            $this->apiError($e->getHttpBody());
            //echo($e->getHttpBody());
        }
    }

    function get_pay_method($method){
        switch ($method) {
            case 'alipay':
                return '支付宝手机';
            case 'alipay_qr':
                return '支付宝扫码';
        }
    }


}